package net.internet;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;

public class HostLookup
{
	public static void main(String[] args)
	{
		if (args.length > 0)
		{
			for (int i = 0; i < args.length; i++)
			{
				System.out.println(lookup(args[i]));
			}
		}
		else
		{
			BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
			System.out.println("enter names and ip addresses.Enter exit to quit");
			try
			{
				while (true)
				{
					String host = in.readLine();
					if (host.equalsIgnoreCase("exit"))
					{
						break;
					}
					System.out.println(lookup(host));
				}
			}
			catch (Exception e)
			{
			}
		}
	}
	
	private static String lookup(String host)
	{
		InetAddress node;
		try
		{
			node = InetAddress.getByName(host);
		}
		catch (Exception e)
		{
			return "cannot find host" + host;
		}
		if (isHostname(host))
		{
			return node.getHostAddress();
		}
		else
		{
			return node.getHostName();
		}
	}
	
	private static boolean isHostname(String host)
	{
		if (host.indexOf(':') != -1)
		{
			return false;
		}
		char[] ca = host.toCharArray();
		for (int i = 0; i < ca.length; i++)
		{
			if (Character.isDigit(ca[i]))
			{
				return true;
			}
		}
		return false;
	}
}
